package com.iss.card.view.overrideview;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import android.util.Log;

import com.iss.card.view.model.CityBean;

public class ParticipatorSortUtils {
	private static final String TAG = ParticipatorSortUtils.class.getSimpleName();

	public static void computeClerkModelSort(List<CityBean> arraylist) {
		if (arraylist == null || arraylist.size() < 1) {
			return;
		}
		ClerkModelCompare compare = new ClerkModelCompare();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (CityBean model : arraylist) {
			String str = doComputeModelFirstLetter(model.getCityName(),defaultFormat);
			model.setJianpin(str);
			model.setAlpha(str.substring(0,1));
		}
		Collections.sort(arraylist, compare);
	}

	/**
	 * ��ȡ����ĸ
	 * 
	 * @param alpha
	 * @param defaultFormat 
	 * @param SourceStr
	 * @return
	 */
	public static String doComputeModelFirstLetter(String sourceStr, HanyuPinyinOutputFormat defaultFormat) {
		String pinyinName = "";
		char[] nameChar = sourceStr.toCharArray();
		try {
			for (int i = 0; i < nameChar.length; i++) {
				if (nameChar[i] > 128) {
//					pinyinName += PinyinHelper.toHanyuPinyinStringArray(
//							nameChar[i], defaultFormat)[0].charAt(0);
					pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];	
				} else {
					pinyinName += nameChar[i];
				}
			}
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			Log.e(TAG, "doComputeModelFirstLetter:BadHanyuPinyinOutputFormatCombination");
			e.printStackTrace();
		}
		//Log.d("kitexy debug", "revert value is ===>" + pinyinName);
		return pinyinName;
	}

	private static class ClerkModelCompare implements Comparator<CityBean> {
		@Override
		public int compare(CityBean object1, CityBean object2) {
			return object1.getAlpha().compareTo(object2.getAlpha());
		}
	}

}
